package com.xjrsoft.module.customer.erp_model.salesOrder.controller;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.helper.DbHelper;
import com.xjrsoft.common.helper.DbHelperFactory;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.sql.SQLException;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.salesOrder.dto.ErpSalesorderDto;
import com.xjrsoft.module.customer.erp_model.salesOrder.dto.SaveErpSalesorderFormDataDto;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.Inventoryinfo;
import com.xjrsoft.module.customer.erp_model.salesOrder.vo.ErpSalesorderFormDataVo;
import com.xjrsoft.module.customer.erp_model.salesOrder.vo.ErpSalesorderListVo;
import com.xjrsoft.module.customer.erp_model.salesOrder.dto.ErpSalesorderListDto;
import com.xjrsoft.module.customer.erp_model.salesOrder.vo.ErpSalesorderentryVo;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorderentry;
import com.xjrsoft.module.customer.erp_model.salesOrder.dto.ErpSalesorderentryDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorder;
import com.xjrsoft.module.customer.erp_model.salesOrder.vo.ErpSalesorderVo;
import com.xjrsoft.module.customer.erp_model.salesOrder.service.IErpSalesorderService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_salesorder")
@Api(value = "销售管理", tags = "销售管理接口")
public class ErpSalesorderController {

	private final IXjrExcelImportService importService;

	private final ObjectMapper objectMapper;

	private final IErpSalesorderService erpSalesorderService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpSalesorder")
	public Response<ErpSalesorderFormDataVo> getFormData(@PathVariable String id) {
		ErpSalesorderFormDataVo formDataVo = new ErpSalesorderFormDataVo();
		// 主表数据
		ErpSalesorder erpSalesorder = erpSalesorderService.getById(id);
		formDataVo.setErpSalesorderVo(BeanUtil.copy(erpSalesorder, ErpSalesorderVo.class));
		// 从表数据
		formDataVo.setErpSalesorderentryVoList(BeanUtil.copyList(erpSalesorderService.getErpSalesorderentryByParentId(id), ErpSalesorderentryVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpSalesorder")
	public Response<PageOutput<ErpSalesorderListVo>> getPageList(ErpSalesorderListDto listDto) {
		IPage<ErpSalesorder> page = erpSalesorderService.getPageList(listDto);
		List<ErpSalesorderListVo> records = BeanUtil.copyList(page.getRecords(), ErpSalesorderListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		//FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpSalesorder")
	public Response save(@RequestBody SaveErpSalesorderFormDataDto formDto) {
		ErpSalesorderDto erpSalesorderDto = formDto.getErpSalesorderDto();
		ErpSalesorder erpSalesorder = BeanUtil.copy(erpSalesorderDto, ErpSalesorder.class);
		List<ErpSalesorderentryDto> erpSalesorderentryDtoList = formDto.getErpSalesorderentryDto();
		List<ErpSalesorderentry> erpSalesorderentryList = BeanUtil.copyList(erpSalesorderentryDtoList, ErpSalesorderentry.class);
		String dataId = erpSalesorderService.addErpSalesorder(erpSalesorder, erpSalesorderentryList);
		if (dataId != null) {
			codeRuleService.useEncode("销售订单单据编号");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpSalesorder")
	public Response update(@PathVariable String id, @RequestBody SaveErpSalesorderFormDataDto formDto) {
		ErpSalesorderDto erpSalesorderDto = formDto.getErpSalesorderDto();
		ErpSalesorder erpSalesorder = BeanUtil.copy(erpSalesorderDto, ErpSalesorder.class);

		List<ErpSalesorderentryDto> erpSalesorderentryDtoList = formDto.getErpSalesorderentryDto();
		List<ErpSalesorderentry> erpSalesorderentryList = BeanUtil.copyList(erpSalesorderentryDtoList, ErpSalesorderentry.class);
		return Response.status(erpSalesorderService.updateErpSalesorder(id, erpSalesorder, erpSalesorderentryList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpSalesorderService.removeByIds(Func.toStrList(ids)));
	}

//	@GetMapping("/getInventory/{goodsId}/{WarehouseId}")
//	@ApiOperation(value = "获取仓库信息")
//	public  Response<Inventoryinfo> getInventory(String goodsId, String WarehouseId) throws SQLException, ClassNotFoundException {
//		return (Response<Inventoryinfo>) erpSalesorderService.getWarehouseInfo(goodsId,WarehouseId);
//	}

	@GetMapping("/getWarehouse")
	@ApiOperation(value = "获取仓库信息")
	public  Response<List<Map<String,Object>>> getWarehouse() throws SQLException, ClassNotFoundException {
		DbHelper dbHelper= DbHelperFactory.getDbHelper(DbType.MYSQL,"localDB");
		return Response.ok(dbHelper.getTableData("select F_Id,F_FullName from erp_warehouse",""));
	}



	
	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<ErpSalesorderDto> entityDtoList = recordList.stream().map(record -> {
				try {
					return objectMapper.readValue(objectMapper.writeValueAsString(record), ErpSalesorderDto.class);
				} catch (JsonProcessingException e) {
				}
				return null;
			}).collect(Collectors.toList());
			List<ErpSalesorder> entities = BeanUtil.copyList(entityDtoList, ErpSalesorder.class);
			return Response.status(erpSalesorderService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
